//
//  ReaderToolBar.m
//  ebook
//
//  Created by 吴超飞 on 16/1/15.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "ReaderToolBar.h"
#import "UIView+Extension.h"
#import "UIImage+RTTint.h"
#import "CDRTranslucentSideBar.h"
#import "BookmarkCatalogView.h"
#import "DownloadManager.h"
#import "SelectChapterView.h"

#define BarHeight (93+ (iPhoneX?34:0))
#define Padding 20
#define BtnCount 4
#define ChapterHeight 50

#define TabbarHeight (52 + (iPhoneX?34:0))

#define ItemsSpace 110//iPad: 两个UITabBarItem间距

@interface ReaderToolBar()<FontSetViewDelegate,UITabBarDelegate,CDRTranslucentSideBarDelegate,BookmarkCatalogViewDelegate,PageStyleViewDelegate, DownloadViewDelegate,UIPopoverControllerDelegate>{
    
    BOOL isShow,isSubShow,isFontShow,isPageStyleShow,isDownloadShow;//视图是否显示、是否有子视图显示
    
    UIView *sliderView;//进度选择视图，独立控制显示、隐藏
    UIView *fixedView;//底部固定的视图(sliderView+tabbar)
    SelectChapterView *chapterView;//滑动显示的章节标题
    FontSetView *fontSetView;
    PageStyleView *pageStyleView;
    NSInteger currentpage,totoalpages;
    
    Chapter *selectChapter;
}

@property (nonatomic,retain) UIButton *preChapterBtn;
@property (nonatomic,retain) UIButton *nextChapterBtn;
@property (nonatomic,retain) UISlider *slider;
@property (nonatomic,retain) UITabBar *tabbar;
@property (nonatomic,retain) CDRTranslucentSideBar *translucentSideBar;
@property (nonatomic,retain) BookmarkCatalogView *bookmarkMenuView;
@property (nonatomic,retain) ReaderBookModel *readerbookModel;
@property (nonatomic,retain) UIPopoverController *popoverController;//ipad弹框
@property (nonatomic,retain) UIViewController *vc;
@property (nonatomic,assign) BOOL isChapterShow;
@property (nonatomic,retain) UITabBarItem *nightItem;

@end


@implementation ReaderToolBar

-(instancetype)initWithDataController:(ReaderBookModel *)readerbookModel{
    
    self = [super initWithFrame:CGRectMake(0, 0, MTScreenW, BarHeight)];
    self.backgroundColor = MTConfigColor(@"MTReaderBar_NightBg_Color");
    if (self) {
        
        _readerbookModel = readerbookModel;
        isShow = YES;
        
        sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MTScreenW, BarHeight - TabbarHeight)];
        fixedView = [[UIView alloc] initWithFrame:self.frame];
        [fixedView addSubview:sliderView];
        [self addSubview:fixedView];
        
        UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(80, 5, MTScreenW - 80*2, 30)];
        _slider = slider;
        slider.maximumValue = 10;
        slider.minimumValue = 1;
        [slider addTarget:self action:@selector(updateSliderValue:) forControlEvents:UIControlEventValueChanged];
        [slider addTarget:self action:@selector(sliderTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        [sliderView addSubview:slider];
        
        self.preChapterBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 70, 30)];
        self.preChapterBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.preChapterBtn setTitle:@"上一章" forState:UIControlStateNormal];
        self.preChapterBtn.tag = 1;
        [self.preChapterBtn addTarget:self action:@selector(chapterChangeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [sliderView addSubview:self.preChapterBtn];
        
        self.nextChapterBtn = [[UIButton alloc] initWithFrame:CGRectMake(MTScreenW - 70, 5, 70, 30)];
        self.nextChapterBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.nextChapterBtn setTitle:@"下一章" forState:UIControlStateNormal];
        self.nextChapterBtn.tag = 2;
        [self.nextChapterBtn addTarget:self action:@selector(chapterChangeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [sliderView addSubview:self.nextChapterBtn];
        
        UITabBar *tabbar = [[UITabBar alloc] initWithFrame:CGRectMake(0, BarHeight - TabbarHeight, MTScreenW+2, TabbarHeight)];
        self.tabbar = tabbar;
        tabbar.delegate = self;
        //清除item的背景色跟选中背景色
        UIImage *bgImg = [[UIImage alloc] init];
        [tabbar setBackgroundImage:bgImg];
        [tabbar setShadowImage:bgImg];
        [tabbar setSelectionIndicatorImage:bgImg];
        
        UITabBarItem *item1 = [self tabbarItemWithTitle:@"目录" imageName:@"catalog_nl" selectedImageName:@"catalog_sl" tag:1];
        UITabBarItem *item2 = [self tabbarItemWithTitle:@"翻页" imageName:@"fanye_nl"selectedImageName:@"fanye_sl" tag:2];
        UITabBarItem *item3 = [self tabbarItemWithTitle:@"设置" imageName:@"zihao_setting_nl" selectedImageName:@"zihao_setting_sl" tag:3];
        UITabBarItem *item4 = [self tabbarItemWithTitle:@"夜间" imageName:@"night_mode" selectedImageName:@"night_mode" tag:4];
        _nightItem = item4;
        UITabBarItem *item5 = [self tabbarItemWithTitle:@"下载" imageName:@"yudu_nl"selectedImageName:@"yudu_sl" tag:5];
        tabbar.items = @[item1,item2,item3,item5,item4];
        [fixedView addSubview:tabbar];
        
        UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MTScreenW, 0.5)];
        topLine.backgroundColor = MTConfigColor(@"MTReaderToolBar_toplineBg_Color");
        [tabbar addSubview:topLine];
        
        fontSetView = [[FontSetView alloc] initWithReaderConfig:[ReaderGlobalModel shareModel].readerConfig];
        fontSetView.delegate = self;
        pageStyleView = [[PageStyleView alloc] initWithReaderConfig:[ReaderGlobalModel shareModel].readerConfig];
        pageStyleView.delegate = self;
        _downloadView = [[DownloadView alloc] initWithReaderConfig:[ReaderGlobalModel shareModel].readerConfig];
        _downloadView.delegate = self;
        
        chapterView = [[SelectChapterView alloc] init];
        chapterView.centerX = self.centerX;
    }
    
    [self setIsNight:_isNight];//设置配色

    return self;
}

- (UITabBarItem *)tabbarItemWithTitle:(NSString *)title imageName:(NSString *)image selectedImageName:(NSString *)selecteImage tag:(NSInteger)tag{
    
    UIColor *selectColor = MTConfigColor(_isNight?@"MTReaderBar_NightSelected_Color":@"MTReaderBar_DaySelected_Color");
    UIColor *normalColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
    
    UIImage *nImg = [[[UIImage imageNamed:image] rt_tintedImageWithColor:normalColor] imageWithRenderingMode:UIImageRenderingModeAutomatic];
    UIImage *sImg = [[[UIImage imageNamed:selecteImage] rt_tintedImageWithColor:selectColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image: nImg selectedImage:sImg];
    
    [item setTitleTextAttributes:@{
                                    NSForegroundColorAttributeName : normalColor
                                    } forState:UIControlStateNormal];
    [item setTitleTextAttributes:@{
                                   NSForegroundColorAttributeName : selectColor
                                   } forState:UIControlStateSelected];
    item.tag = tag;
    
    return item;
}

//上一章、下一章
-(void)chapterChangeBtnClick:(UIButton *)btn{
    
    NSInteger totoal = _readerbookModel.readerBook.chapterList.count;
    if(btn.tag==1&&self.slider.value>0)
        self.slider.value -= 1;
    if(btn.tag==2&&self.slider.value<totoal-1)
        self.slider.value += 1;
    if(self.slider.value<totoal)
        [self updateSliderValue:self.slider];
    [self sliderTouchUpInside:self.slider];
}

-(void)updateSliderValue:(id)sender{
    
    if (!_isChapterShow) {
        [self showChapterView];
    }
    int value = self.slider.value;
    value = value<0?0:value;//value会出现-1的情况，然后报错*** -[__NSArrayM objectAtIndex:]: index 18446744073709551615 beyond bounds [0 .. 1038]
    NSInteger totoal = _readerbookModel.readerBook.chapterList.count;
    if(value < totoal){
        selectChapter = [_readerbookModel.readerBook.chapterList objectAtIndex:value];
    }
    [chapterView updateChapter:selectChapter.chaptername percent:(float)value/totoal];
}

-(void)sliderTouchUpInside:(id)sender{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if ([self.delegate respondsToSelector:@selector(readerToolBar:selectedChapter:)]) {
            [self.delegate readerToolBar:self selectedChapter:selectChapter];
        }
    });
}

//显示章节标题视图
-(void)showChapterView{
    
    self.isChapterShow = YES;
    CGRect frame = chapterView.frame;
    frame.origin.y = -frame.size.height-10;
    chapterView.frame = frame;
    [self addSubview:chapterView];
}

-(void)hideChapterView{
    
    self.isChapterShow = NO;
    [chapterView removeFromSuperview];
}

- (UIPopoverController *)popoverController {
    
    if (!_popoverController) {
        _popoverController = [[UIPopoverController alloc] initWithContentViewController:self.vc];
        _popoverController.delegate = self;
    }
    return _popoverController;
}

- (UIViewController *)vc {
    
    if (!_vc) {
        _vc = [[UIViewController alloc] init];
    }
    return _vc;
}

- (void)deallocPopoverController {
    
    if (_popoverController) {
        _popoverController = nil;
    }
    if (_vc) {
        _vc = nil;
    }
}

#pragma mark - UIPopoverControllerDelegate
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    
    [self hideSubview];
}


-(void)performSelectNightMode{
    self.tabbar.selectedItem = _nightItem;
    [self tabBar:self.tabbar didSelectItem:_nightItem];
}

#pragma mark -

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    switch (tabBar.selectedItem.tag) {
        case 1://目录
        {
            [self showCatelogView];
        }
            break;
        case 2://翻页
        {
            [self showPageStyleView:item];
        }
            break;
        case 3://设置
        {
            [self showFontSetView:item];
        }
            break;
        case 4://夜间模式
        {
            [self changeNightMode];
        }
            break;
        case 5://章节下载
        {
            [self showDownloadView:item];
        }
            break;
        default:
            break;
    }
}

-(void)changeNightMode{
    [self hideSubview];
    _isNight = !_isNight;
    
    if ([self.delegate respondsToSelector:@selector(readerToolBar:setIsNight:)]) {
        [self.delegate readerToolBar:self setIsNight:_isNight];
    }
    
    if(!_isNight)
        [self.tabbar setSelectedItem:nil];
}

-(void)addToView:(UIView *)parentView{
    
    CGRect barFrame = self.frame;
    barFrame.origin.y = parentView.frame.size.height;
    self.frame = barFrame;
    isShow = NO;
    [parentView addSubview:self];
    [parentView bringSubviewToFront:self];
}

-(void)show{
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect origFrame = self.frame;
        origFrame.origin.y = MTScreenH - CGRectGetHeight(origFrame);
        self.frame = origFrame ;
        isShow = YES;
    }];
}

-(void)hide{
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect origFrame = self.frame;
        origFrame.origin.y = origFrame.origin.y + CGRectGetHeight(origFrame);
        self.frame = origFrame ;
    }];
}

-(BOOL)isSHow{
    
    return isShow;
}

//设置总页数
-(void)setTotoalPages:(NSInteger)totoal{
    
    totoalpages = totoal-1;
    self.slider.minimumValue = 0;
    self.slider.maximumValue = totoal-1;
}

//设置当前显示的页数
-(void)setCurrentPage:(NSInteger)page{
    
    currentpage = page;
    [self.slider setValue:page animated:YES];
}

//显示目录视图
-(void)showCatelogView{
    
    if (!_bookmarkMenuView) {
        _bookmarkMenuView = [[BookmarkCatalogView alloc] initWithDataControler:_readerbookModel];
        _bookmarkMenuView.delegate = self;
    }
    
    if(!self.translucentSideBar)
        self.translucentSideBar = [[CDRTranslucentSideBar alloc] init];
    self.translucentSideBar.sideBarWidth = _bookmarkMenuView.width;
    self.translucentSideBar.delegate = self;
    self.translucentSideBar.tag = 0;
    self.translucentSideBar.forbidGes = NO;
    [self.translucentSideBar setContentViewInSideBar:_bookmarkMenuView];
    
    [self.translucentSideBar show];
    if ([self.delegate respondsToSelector:@selector(readerToolBar:isShowBookmarkMenuView:)]) {
        [self.delegate readerToolBar:self isShowBookmarkMenuView:YES];
    }
    
    [self hideSubview];
}

#pragma mark - CDRTranslucentSideBarDelegate
- (void)sideBar:(CDRTranslucentSideBar *)sideBar didAppear:(BOOL)animated {
    
    [_bookmarkMenuView reloadData];
}

-(void)sideBar:(CDRTranslucentSideBar *)sideBar didDisappear:(BOOL)animated{
    
    self.tabbar.selectedItem = nil;
    if ([self.delegate respondsToSelector:@selector(readerToolBar:isShowBookmarkMenuView:)]) {
        [self.delegate readerToolBar:self isShowBookmarkMenuView:NO];
    }
}

#pragma mark -

-(void)hideTopview{
    
    float topHeight = sliderView.height;
    self.height += topHeight;
    self.y -= topHeight;
    fixedView.y += topHeight;
}

-(void)showTopview{
    
    float topHeight = sliderView.height;
    self.height -= topHeight;
    self.y += topHeight;
    fixedView.y -= topHeight;
}

//翻页方式选择
-(void)showPageStyleView:(UITabBarItem *)item{
    
    if (isFontShow) {
        [self showFontSetView:item];
    }
    if (isDownloadShow) {
        [self showDownloadView:item];
    }
    if(_isChapterShow){
        [self hideChapterView];
    }
    
    if (isIpad) {
        
        if (self.popoverController) {
            self.popoverController = nil;
        }
        
        [self deallocPopoverController];
        
        isPageStyleShow = !isPageStyleShow;
        sliderView.hidden = isPageStyleShow;//隐藏顶部进度条选择视图
        
        if (isPageStyleShow) {//显示视图
            [self showTopview];
            [self.vc.view addSubview:pageStyleView];
            [self.vc.view bringSubviewToFront:pageStyleView];
            self.popoverController.backgroundColor = MTConfigColor(_isNight?@"MTReaderBar_NightBg_Color":@"MTReaderBar_DayBg_Color");
            self.popoverController.popoverContentSize = CGSizeMake(pageStyleView.width, pageStyleView.height+20);
            [self.popoverController presentPopoverFromRect:CGRectMake(MTScreenW/2-ItemsSpace, 0, 1, 1) inView:self permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
            self.tabbar.selectedItem = item;
        }else{
            [self hideTopview];
            [self.popoverController dismissPopoverAnimated:YES];
            [pageStyleView removeFromSuperview];
            self.tabbar.selectedItem = nil;
        }
    } else {
        
        isPageStyleShow = !isPageStyleShow;
        sliderView.hidden = isPageStyleShow;//隐藏顶部进度条选择视图
        int offset = pageStyleView.height - 10; //－10往下多偏移一点
        
        if (isPageStyleShow) {//显示翻页方式设置视图
            self.height = self.height + offset;
            self.y = self.y - offset;
            fixedView.y += offset;
            [self addSubview:pageStyleView];
            [self bringSubviewToFront:pageStyleView];
            self.tabbar.selectedItem = item;
        }else{
            self.height = self.height - offset;
            self.y = self.y + offset;
            fixedView.y -= offset;
            [pageStyleView removeFromSuperview];
            self.tabbar.selectedItem = nil;
        }
    }
}

//字号设置
-(void)showFontSetView:(UITabBarItem *)item{
    
    if (isPageStyleShow) {
        [self showPageStyleView:item];
    }
    if (isDownloadShow) {
        [self showDownloadView:item];
    }
    if(_isChapterShow){
        [self hideChapterView];
    }
    
    if (isIpad) {
     
        if (self.popoverController) {
            self.popoverController = nil;
        }
        
        [self deallocPopoverController];
        
        isFontShow = !isFontShow;
        sliderView.hidden = isFontShow;//隐藏顶部进度条选择视图
        
        if (isFontShow) {//显示字体设置视图
            [self showTopview];
            [self.vc.view addSubview:fontSetView];
            [self.vc.view bringSubviewToFront:fontSetView];
            self.popoverController.backgroundColor = MTConfigColor(_isNight?@"MTReaderBar_NightBg_Color":@"MTReaderBar_DayBg_Color");
            if (self.isNight) {
                self.popoverController.popoverContentSize = CGSizeMake(320 + 110, 220);
            } else {
                self.popoverController.popoverContentSize = CGSizeMake(320 + 110, 235+20+35);
            }
            [self.popoverController presentPopoverFromRect:CGRectMake(MTScreenW/2, 0, 1, 1) inView:self permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
            self.tabbar.selectedItem = item;
        }else{
            [self hideTopview];
            [self.popoverController dismissPopoverAnimated:YES];
            [fontSetView removeFromSuperview];
            self.tabbar.selectedItem = nil;
        }
    } else {
        
        isFontShow = !isFontShow;
        sliderView.hidden = isFontShow;//隐藏顶部进度条选择视图
        int offset = fontSetView.height - 30; //－10往下多偏移一点
        
        if (isFontShow) {//显示字体设置视图
            self.height = self.height + offset;
            self.y = self.y - offset;
            fixedView.y += offset;
            [self addSubview:fontSetView];
            [self bringSubviewToFront:fontSetView];
            self.tabbar.selectedItem = item;
        }else{
            self.height = self.height - offset;
            self.y = self.y + offset;
            fixedView.y -= offset;
            [fontSetView removeFromSuperview];
            self.tabbar.selectedItem = nil;
        }
    }
}

//下载设置
- (void)showDownloadView:(UITabBarItem *)item
{
    DownloadModel *model = [[DownloadManager shareInstance] downloadQuequOfBook:self.readerbookModel.readerBook.book.bookid];
    
    //该下载对象不是正在下载的那个，则显示等待中
    if (model&&![model isEqual:[[DownloadManager shareInstance] currentDownload]]) {
        [_downloadView showWaitingStatus];
    }
    [_downloadView setDownloadType:model.downloadType];
    
    if (isPageStyleShow) {
        [self showPageStyleView:item];
    }
    if (isFontShow) {
        [self showFontSetView:item];
    }
    if(_isChapterShow){
        [self hideChapterView];
    }
    
    if (isIpad) {
  
        if (self.popoverController) {
            self.popoverController = nil;
        }
        
        [self deallocPopoverController];
        
        isDownloadShow = !isDownloadShow;
        sliderView.hidden = isDownloadShow;//隐藏顶部进度条选择视图
        
        if (isDownloadShow) {//显示字体设置视图
            [self showTopview];
            [self.vc.view addSubview:_downloadView];
            [self.vc.view bringSubviewToFront:_downloadView];
            self.popoverController.backgroundColor = MTConfigColor(_isNight?@"MTReaderBar_NightBg_Color":@"MTReaderBar_DayBg_Color");
            self.popoverController.popoverContentSize = CGSizeMake(CGRectGetWidth(_downloadView.frame), CGRectGetHeight(_downloadView.frame)+30);
            [self.popoverController presentPopoverFromRect:CGRectMake(MTScreenW/2+ItemsSpace, 0, 1, 1) inView:self permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
            self.tabbar.selectedItem = item;
        }else{
            [self hideTopview];
            [self.popoverController dismissPopoverAnimated:YES];
            [_downloadView removeFromSuperview];
            self.tabbar.selectedItem = nil;
        }
    } else {
        
        isDownloadShow = !isDownloadShow;
        sliderView.hidden = isDownloadShow;//隐藏顶部进度条选择视图
        int offset = _downloadView.height - 10; //－10往下多偏移一点
        
        if (isDownloadShow) {//显示字体设置视图
            self.height = self.height + offset;
            self.y = self.y - offset;
            fixedView.y += offset;
            [self addSubview:_downloadView];
            [self bringSubviewToFront:_downloadView];
            self.tabbar.selectedItem = item;
        }else{
            self.height = self.height - offset;
            self.y = self.y + offset;
            fixedView.y -= offset;
            [_downloadView removeFromSuperview];
            self.tabbar.selectedItem = nil;
        }
    }
}

#pragma  mark - PageStyleViewDelegate
-(void)pageStyleView:(PageStyleView *)view changePageStyle:(PageTurnStyle)style{
    
    if ([self.delegate respondsToSelector:@selector(readerToolBar:changePageStyle:)]) {
        [self.delegate readerToolBar:self changePageStyle:style];
    }
}

- (void)pageStyleView:(PageStyleView *)view autoReadingBtnClick:(UIButton *)btn {
    
    if ([self.delegate respondsToSelector:@selector(readerToolBar:autoReadingBtnClick:)]) {
        [self.delegate readerToolBar:self autoReadingBtnClick:btn];
    }
}

-(void)pageStyleView:(PageStyleView *)view orientationIsLandscape:(BOOL)isLandscape {
  
    //_popoverController iPad中才存在
    [_popoverController dismissPopoverAnimated:NO];
    if ([self.delegate respondsToSelector:@selector(readerToolBar:orientationIsLandscape:)]) {
        [self.delegate readerToolBar:self orientationIsLandscape:isLandscape];
    }
}

#pragma mark - FontSetViewDelegate
-(void)fontSetView:(FontSetView *)view backgroudColor:(ReadBackgroud)bgcolor{
    
    if ([self.delegate respondsToSelector:@selector(readerToolBar:backgroudColor:)]) {
        [self.delegate readerToolBar:self backgroudColor:bgcolor];
    }
}

-(void)fontSetView:(FontSetView *)view fontsizeSet:(NSInteger)fontsize{
    if ([self.delegate respondsToSelector:@selector(readerToolBar:fontsizeSet:)]) {
        [self.delegate readerToolBar:self fontsizeSet:fontsize];
    }
}

-(void)fontSetView:(FontSetView *)view linespace:(CGFloat)space{
    
    if ([self.delegate respondsToSelector:@selector(readerToolBar:linespace:)]) {
        [self.delegate readerToolBar:self linespace:space];
    }
}

- (void)fontSetView:(FontSetView *)view setBtnClick:(NSInteger)btnTag {
    
    if (!isIpad) {
        [self hideSubview];
    }else {
        [self.popoverController dismissPopoverAnimated:YES];
        self.tabbar.selectedItem = nil;
    }
    
    if ([self.delegate respondsToSelector:@selector(readerToolBar:setBtnClick:)]) {
        [self.delegate readerToolBar:self setBtnClick:btnTag];
    }
}

#pragma mark - DownloadViewDelegate
- (void)downloadView:(DownloadView *)view downloadType:(DownloadType)downloadType {
    
    if ([self.delegate respondsToSelector:@selector(readerToolBar:downloadType:)]) {
        [self.delegate readerToolBar:self downloadType:downloadType];
    }
}

- (void)downloadView:(DownloadView *)view downloadCancel:(BOOL)cancel {
    
    if ([self.delegate respondsToSelector:@selector(readerToolBar:downloadCancel:)]) {
        [self.delegate readerToolBar:self downloadCancel:cancel];
    }
}

#pragma mark BookmarkCatalogViewDelegate
-(void)bookmarkCatalogView:(BookmarkCatalogView *)view didSelectedRowObject:(id)obj{
    
    if ([self.delegate respondsToSelector:@selector(readerToolBar:selectedChapter:)]) {
        [self.delegate readerToolBar:self selectedChapter:(Chapter *)obj];
    }
    [self.translucentSideBar dismiss];
}

-(void)bookmarkCatalogViewBack:(BookmarkCatalogView *)view{
    
    [self.translucentSideBar dismiss];
}

-(BOOL)isDownloadViewShow{
    return isDownloadShow;
}

-(BOOL)isShowSubview{
    
    return isPageStyleShow|isFontShow|isDownloadShow|_isChapterShow;
}

-(void)hideSubview{
    
    if (isFontShow) {
        [self showFontSetView:nil];
    }
    
    if (isPageStyleShow) {
        [self showPageStyleView:nil];
    }
    
    if (isDownloadShow) {
        [self showDownloadView:nil];
    }
    
    if (_isChapterShow){
        [self hideChapterView];
    }
}

-(void)setIsNight:(BOOL)isNight{
    
    _isNight = isNight;
    fontSetView.isNight = isNight;
    chapterView.isNight = isNight;
    _downloadView.isNight = isNight;
    pageStyleView.isNight = isNight;
    
    UIColor *selectColor = MTConfigColor(_isNight?@"MTReaderBar_NightSelected_Color":@"MTReaderBar_DaySelected_Color");
    UIColor *normalColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
   
    self.backgroundColor =  MTConfigColor(_isNight?@"MTReaderBar_NightBg_Color":@"MTReaderBar_DayBg_Color");
    
    [self.slider setThumbImage:[[UIImage imageNamed:@"progress_dot_nl"] rt_tintedImageWithColor:selectColor] forState:UIControlStateNormal];
//    [self.slider setMinimumTrackImage:nil forState:UIControlStateNormal];//如果设置了左边的图片就不会显示滑轮左边颜色minimumTrackTintColor了
    
    self.slider.minimumTrackTintColor = selectColor;
    self.slider.maximumTrackTintColor = normalColor;
  
    [self.preChapterBtn setTitleColor:normalColor forState:UIControlStateNormal];
    [self.nextChapterBtn setTitleColor:normalColor forState:UIControlStateNormal];
    
    [self.tabbar setTintColor:normalColor];
    [self.tabbar setSelectedImageTintColor:selectColor];
    
    for(UITabBarItem *item in self.tabbar.items){
        
        [item setTitleTextAttributes:@{
                                       NSForegroundColorAttributeName : normalColor
                                       } forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{
                                       NSForegroundColorAttributeName : selectColor
                                       } forState:UIControlStateSelected];
        [item setImage:[item.image rt_tintedImageWithColor:normalColor]];
    }
}

-(void)cancelDownload{
    
    [_downloadView downloadState];
}

-(void)setProgressPercent:(float)progress exist:(BOOL)exist{
    
    [_downloadView setProgressPercent:progress exist:exist];
}

-(void)showWaitDownload{
    
    [_downloadView showWaitingStatus];
}

-(void)didReceiveMemoryWarningOperator{
    
    [self.bookmarkMenuView didReceiveMemoryWarningOperator];
}

-(void)dealloc{
    
    [self deallocPopoverController];
}

@end
